package com.sinothk.fc.storeOrder.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.fc.bill.entity.BillReq;
import com.sinothk.fc.bill.service.IBillService;
import com.sinothk.fc.goods.entity.GoodsDO;
import com.sinothk.fc.goods.entity.GoodsEntity;
import com.sinothk.fc.goods.service.IGoodsService;
import com.sinothk.fc.goodsStock.entity.GoodsStockEntity;
import com.sinothk.fc.goodsStock.service.IGoodsStockService;
import com.sinothk.fc.goodsStockRecord.service.IGoodsStockRecordService;
import com.sinothk.fc.storeOrder.entity.OrderAddReq;
import com.sinothk.fc.storeOrder.entity.StoreOrderEntity;
import com.sinothk.fc.storeOrder.entity.StoreOrderSearchReq;
import com.sinothk.fc.storeOrder.mapper.StoreOrderMapper;
import com.sinothk.fc.storeOrder.service.IStoreOrderService;
import com.sinothk.fc.storeOrderDetail.entity.StoreOrderDetailEntity;
import com.sinothk.fc.storeOrderDetail.service.IStoreOrderDetailService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.MoneyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @Description: 门店订单表
 * @Author: Liangyt
 * @Date: 2022-09-14
 * @Version: V1.0
 */
@Service
public class StoreOrderServiceImpl extends ServiceImpl<StoreOrderMapper, StoreOrderEntity> implements IStoreOrderService {

    @Resource
    private StoreOrderMapper storeOrderMapper;

    private final IBillService billService;
    private final IGoodsService goodsService;
    private final IGoodsStockService goodsStockService;
    private final IGoodsStockRecordService goodsStockRecordService;

    private final IStoreOrderDetailService storeOrderDetailService;

    public StoreOrderServiceImpl(IBillService billService, IGoodsService goodsService, IGoodsStockService goodsStockService, IGoodsStockRecordService goodsStockRecordService, IStoreOrderDetailService storeOrderDetailService) {
        this.billService = billService;
        this.goodsService = goodsService;
        this.goodsStockService = goodsStockService;
        this.goodsStockRecordService = goodsStockRecordService;
        this.storeOrderDetailService = storeOrderDetailService;
    }

    @Override
    public IPage<StoreOrderEntity> pageForStoreGroupByOrderId(Page<StoreOrderEntity> page, StoreOrderSearchReq searchReq) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        return storeOrderMapper.pageForStoreGroupByOrderId(page, loginUser.getStoreId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPayInfoForSale(OrderAddReq<StoreOrderDetailEntity> addReq) throws Exception {
//        if (addReq == null) throw new Exception("请求参数不能为空");
//        if (addReq.getBizData() == null) throw new Exception("销售信息不能为空");
//        if (addReq.getOrderIncomePrice() == null) throw new Exception("订单实收总价不能为空");
//        if (addReq.getOrderFlagAllPrice() == null) throw new Exception("订单标价总价不能为空");
//
//        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
//
//        // 新增订单信息-实体店销售
//        StoreOrderEntity storeOrder = new StoreOrderEntity();
//        storeOrder.setStoreId(loginUser.getStoreId());
//        storeOrder.setOrderIncomePrice(addReq.getOrderIncomePrice());// 订单实收总价
//        storeOrder.setOrderFlagAllPrice(addReq.getOrderFlagAllPrice());//订单标价总价
//        storeOrder.setCustomerId("");
//        storeOrder.setOrderDesc("销售");
//        storeOrder.setState(addReq.getState());//状态:0待缴费1已缴费
//        this.save(storeOrder);
//
//        // 保存销售记录-实体店销售
//        // 当 实际收入和应该收入不等时，计算百分比
//        float p = -1;
//        if (!addReq.getOrderIncomePrice().equals(addReq.getOrderFlagAllPrice())) {
//            p = addReq.getOrderIncomePrice() * 1.00f / addReq.getOrderFlagAllPrice() * 1.00f;
//        }
//        if (addReq.getBizData().size() > 0 && StringUtils.isNotBlank(storeOrder.getId())) {
//
//            int size = addReq.getBizData().size();
//            long upAllValue = 0;
//            List<GoodsEntity> goodsList = new ArrayList<>();
//            for (int i = 0; i < size; i++) {
//                // 新增订单销售记录
//                StoreOrderRecordEntity orderRecord = addReq.getBizData().get(i);
//                orderRecord.setOrderId(storeOrder.getId());
//                orderRecord.setStoreId(loginUser.getStoreId());
//                if (p != -1) {// 当 实际收入和应该收入不等时，计算百分比
//                    if (i != size - 1) {
//                        long price = (long) (orderRecord.getGoodsFlagPrice() * 1.00f * p);
//                        orderRecord.setGoodsIncomePrice(price);
//                        upAllValue += price;
//                    } else {
//                        orderRecord.setGoodsIncomePrice(addReq.getOrderIncomePrice() - upAllValue);
//                    }
//                } else {
//                    orderRecord.setGoodsIncomePrice(orderRecord.getGoodsFlagPrice());
//                }
//
//                GoodsEntity goods = goodsService.getById(orderRecord.getGoodsId());
//                int saleNum = orderRecord.getGoodsNum();
////                goods.setStockNum(goods.getStockNum() - saleNum);
//                long saleNumDb = goods.getSaleNum() == null ? 0 : goods.getSaleNum();
//                goods.setSaleNum(saleNumDb + saleNum);
//                goodsList.add(goods);
//            }
//            goodsService.updateBatchById(goodsList);
//            storeSaleRecordService.saveBatch(addReq.getBizData());
//
//            // 创建账单 分开插入是为等上步完成后，以便获取记录ID
//            ArrayList<BillEntity> billList = new ArrayList<>();
//            for (int i = 0; i < size; i++) {
//                // 新增订单销售记录
//                StoreOrderRecordEntity orderRecord = addReq.getBizData().get(i);
////                // 新增账单-实体店销售
////                BillEntity bill = new BillEntity();
////                bill.setStoreId(loginUser.getStoreId());
////                bill.setOrderId(storeOrder.getId());
////                bill.setOrderRecordId(orderRecord.getId());
////                bill.setBillValue(orderRecord.getGoodsIncomePrice());
////                bill.setBillFlagValue(orderRecord.getGoodsFlagPrice());
////                bill.setBillState(0); // 0收入1支出2商家退回款
////                bill.setGoodsId(orderRecord.getGoodsId());
////                bill.setBillName("销售-" + orderRecord.getGoodsName());
////                bill.setBillLogo(orderRecord.getGoodsCover());
////                bill.setBillType("销售收入");
////                bill.setBillMonth(DateUtil.format(new Date(), "yyyy年MM月"));
////                billList.add(bill);
//            }
//            // 批量插入
//            billService.saveBatch(billList);
//        }
    }

    @Override
    public IPage<StoreOrderEntity> listForStore(Page<StoreOrderEntity> page, StoreOrderSearchReq searchReq) throws Exception {
        LambdaQueryWrapper<StoreOrderEntity> wrapper = new LambdaQueryWrapper<>();
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        wrapper.eq(StoreOrderEntity::getStoreId, loginUser.getStoreId());
        wrapper.orderByDesc(StoreOrderEntity::getCreateTime);

        IPage<StoreOrderEntity> pageData = this.page(page, wrapper);
        if (pageData != null && pageData.getRecords() != null && pageData.getRecords().size() > 0) {

            for (int i = 0; i < pageData.getRecords().size(); i++) {
                StoreOrderEntity storeOrder = pageData.getRecords().get(i);
                LambdaQueryWrapper<StoreOrderDetailEntity> wrapperForOrderRecord = new LambdaQueryWrapper<>();
                wrapperForOrderRecord.eq(StoreOrderDetailEntity::getStoreId, loginUser.getStoreId());
//                if (StringUtils.isNotBlank(searchReq.getKeywords())) {
//                    wrapperForOrderRecord.likeLeft(StoreOrderRecordEntity::getGoodsName, searchReq.getKeywords());
//                }
                wrapperForOrderRecord.orderByDesc(StoreOrderDetailEntity::getCreateTime);
//                List<StoreOrderRecordEntity> orderRecords = storeSaleRecordService.list(wrapperForOrderRecord);
//                storeOrder.setOrderRecords(orderRecords);
            }
        }
        return pageData;
    }

    /**
     * 实体店商品退货
     *
     * @param storeRecordId
     * @throws Exception
     */
    @Override
    public void storeOrderRemove(String storeRecordId) throws Exception {
//        StoreOrderRecordEntity orderRecord = storeSaleRecordService.getById(storeRecordId);
//        if (orderRecord == null) throw new Exception("销售记录[" + storeRecordId + "]不存在");
//
//        // 逻辑删除销售记录
//        orderRecord.setState(1);// 状态:0正常1退货
//        storeSaleRecordService.updateById(orderRecord);

//        // 设置账单退款
//        LambdaUpdateWrapper<BillEntity> billWrapper = new LambdaUpdateWrapper<>();
//        billWrapper.eq(BillEntity::getOrderRecordId, orderRecord.getId());
//        billWrapper.set(BillEntity::getBillState, 2);//账单：0收入1支出2商家退回款
//        billService.update(billWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void storeOrderForBuy(OrderAddReq<GoodsStockEntity> beanReq) throws Exception {
//        if (beanReq == null) throw new Exception("请求参数不能为空");
//        if (beanReq.getBizData() == null) throw new Exception("进货信息不能为空");
//        if (beanReq.getOrderIncomePrice() == null) throw new Exception("进货实付总价不能为空");
//        if (beanReq.getOrderFlagAllPrice() == null) throw new Exception("进货订单标价总价不能为空");
//
//        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
//        // 新增订单信息-实体店销售
//        StoreOrderEntity storeOrder = new StoreOrderEntity();
//        storeOrder.setStoreId(loginUser.getStoreId());
//        storeOrder.setOrderIncomePrice(beanReq.getOrderIncomePrice());// 订单实收总价
//        storeOrder.setOrderFlagAllPrice(beanReq.getOrderFlagAllPrice());//订单标价总价
//        storeOrder.setOrderDesc("进货支出");
//        storeOrder.setCustomerId("");
//        storeOrder.setState(beanReq.getState());//状态:0待缴费1已缴费
//        this.save(storeOrder);
//
//        // 保存库存
//        if (beanReq.getBizData().size() > 0 && StringUtils.isNotBlank(storeOrder.getId())) {
//            goodsStockService.saveBatch(beanReq.getBizData());
//
//            // 批量插入进货记录
//            ArrayList<GoodsStockRecordEntity> stockRecordList = new ArrayList<>();
//            int size = beanReq.getBizData().size();
//            for (int i = 0; i < size; i++) {
//                GoodsStockEntity goodsStock = beanReq.getBizData().get(i);
//                // 新增订单销售记录
//                GoodsStockRecordEntity orderRecord = new GoodsStockRecordEntity();
//                orderRecord.setStoreId(goodsStock.getStoreId());
//                orderRecord.setStockId(storeOrder.getId());
//
//                orderRecord.setSellerId(beanReq.getBillUserId());
//                orderRecord.setSellerName(beanReq.getBillUserName());
//                orderRecord.setSellerLogo(beanReq.getBillUserAvatar());
//                orderRecord.setSellerPhone(beanReq.getBillUserPhone());
//
//                orderRecord.setGoodsName(goodsStock.getGoodsName());
//                orderRecord.setGoodsCover(goodsStock.getGoodsCover());
//                orderRecord.setGoodsInPrice(goodsStock.getGoodsInPrice());
//                orderRecord.setStockNum(goodsStock.getStockNum());
//                orderRecord.setGoodsAllPrice(goodsStock.getGoodsFlagPrice());
//                orderRecord.setGoodsUnit(goodsStock.getGoodsUnit());
//
//                orderRecord.setCpScrq(goodsStock.getCpScrq());
//                orderRecord.setCpBzq(goodsStock.getCpBzq());
//
//                orderRecord.setState(0);// 状态:0正常1退货
//                stockRecordList.add(orderRecord);
//            }
//            goodsStockRecordService.saveBatch(stockRecordList);
//
//            // 创建账单 分开插入是为等上步完成后，以便获取记录ID
//            ArrayList<BillEntity> billList = new ArrayList<>();
//            for (int i = 0; i < size; i++) {
////                // 新增订单销售记录
////                GoodsStockEntity goodsStock = beanReq.getBizData().get(i);
////                // 新增账单-实体店销售
////                BillEntity bill = new BillEntity();
////                bill.setStoreId(loginUser.getStoreId());
////                bill.setOrderId(storeOrder.getId());
////                bill.setOrderRecordId(goodsStock.getId());
////                bill.setBillValue(goodsStock.getGoodsFlagPrice() * -1);
////                bill.setBillFlagValue(goodsStock.getGoodsFlagPrice() * -1);
////                bill.setBillState(1); // 0收入1支出2商家退回款
////                bill.setGoodsId(goodsStock.getId());
////                bill.setBillName("进货-" + goodsStock.getGoodsName());
////                bill.setBillLogo(goodsStock.getGoodsCover());
////                bill.setBillType("进货支出");
////                bill.setBillMonth(DateUtil.format(new Date(), "yyyy年MM月"));
////                billList.add(bill);
//            }
//            // 批量插入
//            billService.saveBatch(billList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSaleOrder(OrderAddReq<GoodsDO> addReq) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店信息");
        if (addReq == null) throw new Exception("订单信息不能为空");

        // 销售模式：0计件销售、1多件组合销售、2称重销售
        if (addReq.getOrderModel() == 0) { // 0计件销售
            zhengTiOrderAdd(addReq);
        } else if (addReq.getOrderModel() == 1) { // 1多件组合销售、组合
            zuHeOrderAdd(addReq);
        } else if (addReq.getOrderModel() == 2) { // 2称重销售
            chengZhongOrderAdd(addReq);
        }
    }

    private void zhengTiOrderAdd(OrderAddReq<GoodsDO> addReq) {
        ArrayList<StoreOrderDetailEntity> orderDetailList = new ArrayList<>();
        ArrayList<BillReq> billListReq = new ArrayList<>();

        StoreOrderEntity storeOrder = new StoreOrderEntity();
        BeanUtils.copyProperties(addReq, storeOrder);
        storeOrder.setState(1);//状态:0待缴费1已缴费2赊账9退款
        storeOrder.setOrderDesc("商品售出");
        // 交易者信息
        if (addReq.getTraderBean() != null) {
            storeOrder.setTraderId(addReq.getTraderBean().getTraderId());
            storeOrder.setTraderName(addReq.getTraderBean().getTraderName());
            storeOrder.setTraderAvatar(addReq.getTraderBean().getTraderLogo());
            storeOrder.setTraderPhone(addReq.getTraderBean().getTraderPhone());
        }

        StringBuilder orderName = new StringBuilder();
        StringBuilder orderDesc = new StringBuilder();
        long goodsAllPrice = 0L;
        for (int i = 0; i < addReq.getBizData().size(); i++) {
            // 订单信息
            GoodsDO goodsDO = addReq.getBizData().get(i);
            if (i == 0) {
                goodsAllPrice = goodsDO.getGoodsAllPrice().longValue();
                orderName = new StringBuilder(goodsDO.getGoodsName());
                orderDesc = new StringBuilder(goodsDO.getGoodsName() + "[" + MoneyUtil.getPriceValueStr(goodsDO.getGoodsSalePrice()) + "x" + +goodsDO.getGoodsNum() + "]");
            } else {
                goodsAllPrice += goodsDO.getGoodsAllPrice().longValue();
                orderName.append("+").append(goodsDO.getGoodsName());
                orderDesc.append("+").append(goodsDO.getGoodsName()).append("[").append(MoneyUtil.getPriceValueStr(goodsDO.getGoodsSalePrice())).append("x").append(goodsDO.getGoodsNum()).append("]");
            }
        }
        storeOrder.setOrderName(orderName.toString());
        storeOrder.setOrderDesc(orderDesc.toString());
        storeOrder.setOrderFlagAllPrice(goodsAllPrice);
        storeOrder.setOrderIncomePrice(storeOrder.getOrderFlagAllPrice());
        this.save(storeOrder);

        // 新增记录
        for (int i = 0; i < addReq.getBizData().size(); i++) {
            // 订单信息
            GoodsDO goodsDO = addReq.getBizData().get(i);
            // 订单记录
            StoreOrderDetailEntity storeOrderDetail = new StoreOrderDetailEntity();
            storeOrderDetail.setGoodsId(goodsDO.getId());
            storeOrderDetail.setGoodsName(goodsDO.getGoodsName());
            storeOrderDetail.setGoodsCover(goodsDO.getGoodsCover());
            storeOrderDetail.setGoodsPrice(goodsDO.getGoodsFlagPrice());
            storeOrderDetail.setGoodsFlagPrice(goodsDO.getGoodsAllPrice().longValue());// 应收
            storeOrderDetail.setGoodsIncomePrice(goodsDO.getGoodsAllPrice().longValue());// 实收
            storeOrderDetail.setGoodsNum(goodsDO.getGoodsNum());
            storeOrderDetail.setGoodsPrice(goodsDO.getGoodsFlagPrice());// 标价

            storeOrderDetail.setOrderId(storeOrder.getId());
            storeOrderDetail.setOrderName(storeOrder.getOrderName());
            storeOrderDetail.setTraderId(storeOrder.getTraderId());
            storeOrderDetail.setTraderName(storeOrder.getTraderName());
            storeOrderDetail.setTraderAvatar(storeOrder.getTraderAvatar());
            storeOrderDetail.setTraderPhone(storeOrder.getTraderPhone());
            orderDetailList.add(storeOrderDetail);

            // 加入账单
            BillReq billReq = new BillReq();
            billReq.setBillState(BillReq.PAY_TYPE_INCOME); //账单：0收入1支出
            billReq.setBillFlagValue(goodsDO.getGoodsAllPrice().longValue());
            billReq.setBillValue(billReq.getBillFlagValue());//
            billReq.setBillName("销售-" + goodsDO.getGoodsName());
            billReq.setBillLogo(goodsDO.getGoodsCover());
            billReq.setBillType("销售收入");
            billReq.setBizId(goodsDO.getId());
            billReq.setBizType(BillReq.BIZ_TYPE_GOODS_SALE);

            billReq.setOrderId(storeOrder.getId());
            billReq.setOrderDesc(storeOrder.getOrderDesc());
            billReq.setBillUserId(storeOrder.getTraderId());
            billReq.setBillUserName(storeOrder.getTraderName());
            billReq.setBillUserAvatar(storeOrder.getTraderAvatar());
            billReq.setBillUserPhone(storeOrder.getTraderPhone());
            billListReq.add(billReq);
        }
        storeOrderDetailService.saveBatch(orderDetailList);
        billService.doBathPay(billListReq);
    }

    private void chengZhongOrderAdd(OrderAddReq<GoodsDO> addReq) {
        ArrayList<StoreOrderDetailEntity> orderDetailList = new ArrayList<>();
        ArrayList<BillReq> billListReq = new ArrayList<>();

        StoreOrderEntity storeOrder = new StoreOrderEntity();
        BeanUtils.copyProperties(addReq, storeOrder);
        storeOrder.setState(1);//状态:0待缴费1已缴费2赊账9退款
        storeOrder.setOrderDesc("商品售出");
        // 交易者信息
        if (addReq.getTraderBean() != null) {
            storeOrder.setTraderId(addReq.getTraderBean().getTraderId());
            storeOrder.setTraderName(addReq.getTraderBean().getTraderName());
            storeOrder.setTraderAvatar(addReq.getTraderBean().getTraderLogo());
            storeOrder.setTraderPhone(addReq.getTraderBean().getTraderPhone());
        }

        StringBuilder orderName = new StringBuilder();
        StringBuilder orderDesc = new StringBuilder();
        long goodsAllPrice = 0L;
        for (int i = 0; i < addReq.getBizData().size(); i++) {
            // 订单信息
            GoodsDO goodsDO = addReq.getBizData().get(i);
            if (i == 0) {
                goodsAllPrice = goodsDO.getGoodsAllPrice().longValue();
                orderName = new StringBuilder(goodsDO.getGoodsName());
                orderDesc = new StringBuilder(goodsDO.getGoodsName() + "[" + MoneyUtil.getPriceValueStr(goodsDO.getGoodsSalePrice()) + "x" + +goodsDO.getGoodsNum() + "]");
            } else {
                goodsAllPrice += goodsDO.getGoodsAllPrice().longValue();
                orderName.append("+").append(goodsDO.getGoodsName());
                orderDesc.append("+").append(goodsDO.getGoodsName()).append("[").append(MoneyUtil.getPriceValueStr(goodsDO.getGoodsSalePrice())).append("x").append(goodsDO.getGoodsNum()).append("]");
            }
        }
        storeOrder.setOrderName(orderName.toString());
        storeOrder.setOrderDesc(orderDesc.toString());
        storeOrder.setOrderFlagAllPrice(goodsAllPrice);
        storeOrder.setOrderIncomePrice(storeOrder.getOrderFlagAllPrice());
        this.save(storeOrder);

        // 新增记录
        for (int i = 0; i < addReq.getBizData().size(); i++) {
            // 订单信息
            GoodsDO goodsDO = addReq.getBizData().get(i);
            // 订单记录
            StoreOrderDetailEntity storeOrderDetail = new StoreOrderDetailEntity();
            storeOrderDetail.setGoodsId(goodsDO.getId());
            storeOrderDetail.setGoodsName(goodsDO.getGoodsName());
            storeOrderDetail.setGoodsCover(goodsDO.getGoodsCover());
            storeOrderDetail.setGoodsPrice(goodsDO.getGoodsFlagPrice());
            storeOrderDetail.setGoodsFlagPrice(goodsDO.getGoodsAllPrice().longValue());// 应收
            storeOrderDetail.setGoodsIncomePrice(goodsDO.getGoodsAllPrice().longValue());// 实收
            storeOrderDetail.setGoodsNum(goodsDO.getGoodsNum());
            storeOrderDetail.setGoodsPrice(goodsDO.getGoodsFlagPrice());// 标价

            storeOrderDetail.setOrderId(storeOrder.getId());
            storeOrderDetail.setOrderName(storeOrder.getOrderName());
            storeOrderDetail.setTraderId(storeOrder.getTraderId());
            storeOrderDetail.setTraderName(storeOrder.getTraderName());
            storeOrderDetail.setTraderAvatar(storeOrder.getTraderAvatar());
            storeOrderDetail.setTraderPhone(storeOrder.getTraderPhone());
            orderDetailList.add(storeOrderDetail);

            // 加入账单
            BillReq billReq = new BillReq();
            billReq.setBillState(BillReq.PAY_TYPE_INCOME); //账单：0收入1支出
            billReq.setBillFlagValue(goodsDO.getGoodsAllPrice().longValue());
            billReq.setBillValue(billReq.getBillFlagValue());//
            billReq.setBillName("销售-" + goodsDO.getGoodsName());
            billReq.setBillLogo(goodsDO.getGoodsCover());
            billReq.setBillType("销售收入");
            billReq.setBizId(goodsDO.getId());
            billReq.setBizType(BillReq.BIZ_TYPE_GOODS_SALE);

            billReq.setOrderId(storeOrder.getId());
            billReq.setOrderDesc(storeOrder.getOrderDesc());
            billReq.setBillUserId(storeOrder.getTraderId());
            billReq.setBillUserName(storeOrder.getTraderName());
            billReq.setBillUserAvatar(storeOrder.getTraderAvatar());
            billReq.setBillUserPhone(storeOrder.getTraderPhone());
            billListReq.add(billReq);
        }
        storeOrderDetailService.saveBatch(orderDetailList);
        billService.doBathPay(billListReq);
    }

    private void zuHeOrderAdd(OrderAddReq<GoodsDO> addReq) {// 组合订单提交
        for (GoodsDO goodsDO : addReq.getBizData()) {
            if (StringUtils.isNotBlank(goodsDO.getId())) {
                // =============================== 订单商品
                StoreOrderEntity storeOrder = new StoreOrderEntity();
                BeanUtils.copyProperties(addReq, storeOrder);
                storeOrder.setId(UUID.fastUUID().toString());
                storeOrder.setState(1);//状态:0待缴费1已缴费2赊账9退款
                storeOrder.setOrderName(goodsDO.getGoodsName());
                storeOrder.setOrderDesc("商品售出收入");
                // 交易者信息
                if (addReq.getTraderBean() != null) {
                    storeOrder.setTraderId(addReq.getTraderBean().getTraderId());
                    storeOrder.setTraderName(addReq.getTraderBean().getTraderName());
                    storeOrder.setTraderAvatar(addReq.getTraderBean().getTraderLogo());
                    storeOrder.setTraderPhone(addReq.getTraderBean().getTraderPhone());
                }
                // 订单总价
                storeOrder.setGoodsSalePrice(goodsDO.getGoodsSalePrice()); //订单单价
                storeOrder.setGoodsNum(goodsDO.getGoodsNum());// 点单数
                storeOrder.setOrderIncomePrice(goodsDO.getGoodsAllPrice().longValue());// 订单实收总价
                storeOrder.setOrderFlagAllPrice(storeOrder.getOrderIncomePrice());// 订单标价总价
                this.save(storeOrder);
                // =============================== 订单商品

                ArrayList<StoreOrderDetailEntity> orderDetailList = new ArrayList<>();
                ArrayList<BillReq> billListReq = new ArrayList<>();
                String[] idsArr = goodsDO.getId().split(",");
                for (int i = 0; i < idsArr.length; i++) {
                    GoodsEntity goodsDB = goodsService.getById(idsArr[i]);
                    // 订单详情=========
                    StoreOrderDetailEntity storeOrderDetail = new StoreOrderDetailEntity();
                    storeOrderDetail.setOrderId(storeOrder.getId());
                    storeOrderDetail.setOrderName(storeOrder.getOrderName());
                    storeOrderDetail.setTraderId(storeOrder.getTraderId());
                    storeOrderDetail.setTraderName(storeOrder.getTraderName());
                    storeOrderDetail.setTraderAvatar(storeOrder.getTraderAvatar());
                    storeOrderDetail.setTraderPhone(storeOrder.getTraderPhone());

                    // 查表数据
                    storeOrderDetail.setGoodsId(goodsDB.getId());
                    storeOrderDetail.setGoodsName(goodsDB.getGoodsName());
                    storeOrderDetail.setGoodsCover(goodsDB.getGoodsCover());
                    storeOrderDetail.setGoodsInPrice(goodsDB.getGoodsInPrice()); // 成本价格
                    storeOrderDetail.setGoodsPrice(goodsDB.getGoodsFlagPrice()); // 原销售单价

                    // 计算单品销售数据
                    storeOrderDetail.setGoodsSalePrice(goodsDB.getGoodsSalePrice());// 标价
                    storeOrderDetail.setGoodsNum(goodsDO.getGoodsNum());
                    long allPrice = (long) (storeOrderDetail.getGoodsSalePrice() * storeOrderDetail.getGoodsNum());
                    storeOrderDetail.setGoodsFlagPrice(allPrice);// 标价总价
                    storeOrderDetail.setGoodsIncomePrice(allPrice);// 实收总价
                    orderDetailList.add(storeOrderDetail);

                    // 加入账单
                    BillReq billReq = new BillReq();
                    billReq.setBillState(BillReq.PAY_TYPE_INCOME); //账单：0收入1支出
                    billReq.setBillValue(storeOrderDetail.getGoodsIncomePrice());// 实收
                    billReq.setBillFlagValue(storeOrderDetail.getGoodsIncomePrice()); // 应收

                    billReq.setBillName("销售-" + storeOrderDetail.getGoodsName());
                    billReq.setBillLogo(storeOrderDetail.getGoodsCover());
                    billReq.setBillType("销售收入");
                    billReq.setBizId(storeOrderDetail.getGoodsId());
                    billReq.setBizType(BillReq.BIZ_TYPE_GOODS_SALE);

                    billReq.setOrderId(storeOrder.getId());
                    billReq.setOrderDesc(storeOrder.getOrderName());

                    billReq.setBillUserId(storeOrder.getTraderId());
                    billReq.setBillUserName(storeOrder.getTraderName());
                    billReq.setBillUserAvatar(storeOrder.getTraderAvatar());
                    billReq.setBillUserPhone(storeOrder.getTraderPhone());
                    billListReq.add(billReq);
                }
                storeOrderDetailService.saveBatch(orderDetailList);
                billService.doBathPay(billListReq);
            }
        }
    }

//    private void addOrderDetail(StoreOrderEntity storeOrder, GoodsEntity saleGoods) throws Exception {
//        StoreOrderDetailEntity storeOrderDetail = new StoreOrderDetailEntity();
//        storeOrderDetail.setOrderId(storeOrder.getId());
//        // 商品信息
//        storeOrderDetail.setGoodsId(saleGoods.getId());
//        storeOrderDetail.setGoodsName(saleGoods.getGoodsName());
//        storeOrderDetail.setGoodsCover(saleGoods.getGoodsCover());
//        storeOrderDetail.setGoodsFlagPrice(saleGoods.getGoodsSalePrice());// 应收
//        storeOrderDetail.setGoodsIncomePrice(saleGoods.getGoodsSalePrice());// 实收
//        storeOrderDetail.setGoodsNum(1);
//        storeOrderDetail.setGoodsPrice(saleGoods.getGoodsSalePrice());// 标价
//        // 交易对象
//        storeOrderDetail.setTraderId(storeOrder.getTraderId());
//        storeOrderDetail.setTraderName(storeOrder.getTraderName());
//        storeOrderDetail.setTraderAvatar(storeOrder.getTraderAvatar());
//        storeOrderDetail.setTraderPhone(storeOrder.getTraderPhone());
//
//        storeOrderDetailService.save(storeOrderDetail);
//
//        // 加入账单
//        BillReq billReq = new BillReq();
//        billReq.setBillState(BillReq.PAY_TYPE_INCOME); //账单：0收入1支出
//        billReq.setBillFlagValue(saleGoods.getGoodsSalePrice());
//        billReq.setBillValue(saleGoods.getGoodsSalePrice());//
//        billReq.setBillName("销售-" +saleGoods.getGoodsName());
//        billReq.setBillLogo(saleGoods.getGoodsCover());
//        billReq.setBillType("销售收入");
//        billReq.setOrderId(storeOrder.getId());
//        billReq.setBizId(saleGoods.getId());
//        billReq.setBizType(BillReq.BIZ_TYPE_GOODS_SALE);
//
//        // 收款方
//        billReq.setBillUserId(storeOrder.getTraderId());
//        billReq.setBillUserName(storeOrder.getTraderName());
//        billReq.setBillUserAvatar(storeOrder.getTraderAvatar());
//        billReq.setBillUserPhone(storeOrder.getTraderPhone());
//
//        billService.doPay(billReq);
//    }
}
